<template>
  <div class="login-container">
    <el-form ref="formRef" :model="form" class="login-form" :rules="rules">
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
      </div>
      <el-form-item prop="username">
        <span class="iconfont icon-zhanghu icon-container"></span>
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="iconfont icon-suoding icon-container"></span>
        <el-input v-model="form.password" :type="passwordType" />
        <div @click="changEye" class="eye">
          <span class="iconfont icon-yanjing_yincang_o" v-show="passwordType === 'password'"></span>
          <span class="iconfont icon-yanjing" v-show="passwordType === 'text'"></span>
        </div>
      </el-form-item>
      <el-button type="primary" class="login-button" @click="handleLogin">{{ $t('login.btnTitle') }}</el-button>
    </el-form>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { user } from '@/store/app'
const userStore = user()
let { token } = storeToRefs(userStore)
// 定义账号和密码
const form = ref({
  username: '17684161829',
  password: '123456'
})
const passwordType = ref('password')
const changEye = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}
//账号和密码的校验
const rules = ref({
  username: [
    { required: true, message: '请输入11位账号' },
    { min: 11, max: 11, message: '号码只能为11位' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码只能为6到20位', trigger: 'blur' },
  ],
})
const formRef = ref(null)
// 登录进行统一校验
const handleLogin = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // alert('登录成功！')
      // let res = await login(form.value)
      // console.log(res);
      userStore.login(form.value)
    } else {
      console.log('失败的提交')
      return false
    }
  })
}

</script>

<style lang="scss" scoped>
@import url('http://at.alicdn.com/t/c/font_3828358_vibw4lpmm2.css');
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

::v-deep .el-input__wrapper {
  width: 100%;
  border: 0px;
  background-color: transparent;
  box-shadow: 0 0 0 0px var(--el-input-border-color, var(--el-border-color)) inset;
  cursor: default;
  .el-input__inner {
    cursor: default !important;
  }
}

.eye {
  color: #fff;
}

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(220, 67, 67, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        color: $light_gray;
        width: 100%;
        height: 45px;
        caret-color: $cursor;
      }
    }

    .login-button {
      width: 100%;
      box-sizing: border-box;
    }
  }

  .tips {
    font-size: 16px;
    line-height: 28px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .icon-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    font-size: 2em;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    ::v-deep .lang-select {
      position: absolute;
      top: 4px;
      right: 0;
      background-color: white;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>